<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Develop;

use Srv\Apps\Apis\Develop\Ctl\GlobalConfCtl;
use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Proto\DTO\Develop\AppAdDayLimitDTO;
use Srv\Apps\Proto\DTO\Develop\AppAdOpenDTO;
use Srv\Apps\Proto\DTO\Develop\AppAdSplitTimeDTO;
use Srv\Apps\Proto\DTO\Develop\AppBannerShowStyleDTO;
use Srv\Apps\Proto\DTO\Develop\AppGlobalConfDTO;
use Srv\Apps\Proto\DTO\Develop\AppPayRechargeDTO;
use Srv\Apps\Proto\DTO\Develop\AppPayRechargeTextDTO;
use Srv\Apps\Proto\DTO\Develop\AppPayVipDTO;
use Srv\Apps\Proto\DTO\Develop\AppTaskConfDTO;
use Srv\Apps\Proto\DTO\Develop\GlobalConfAddDTO;
use Srv\Apps\Proto\DTO\Develop\GlobalConfDTO;
use Srv\Apps\Proto\DTO\Develop\GlobalConfEditDTO;
use Srv\Apps\Proto\DTO\Develop\GlobalConfListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Develop\AppGlobalConfVO;
use Srv\Apps\Proto\VO\Develop\GlobalConfListVO;
use Srv\Apps\Proto\VO\Develop\GlobalConfVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class GlobalConfAct extends ActionAbstract
{
    /**
     * @url /global/conf/list
     * @auth globalConfList
     * @req Srv.Apps.Proto.DTO.Develop.GlobalConfListDTO
     * @rsp Srv.Apps.Proto.VO.Develop.GlobalConfListVO
     * 全局配置列表
     */
    public function globalConfList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof GlobalConfListDTO) || !($VOClass instanceof GlobalConfListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = GlobalConfCtl::tag()->getGlobalConfList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /global/conf/info
     * @auth globalConfInfo
     * @req Srv.Apps.Proto.DTO.Develop.GlobalConfDTO
     * @rsp Srv.Apps.Proto.VO.Develop.GlobalConfVO
     * 全局配置信息
     */
    public function globalConfInfo(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($RouteConfData);
        if(!($DTOClass instanceof GlobalConfDTO) || !($VOClass instanceof GlobalConfVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $stgsId     = intval($DTOClass->getStgsId());
        $VOClass    = GlobalConfCtl::tag()->getGlobalConfInfo($stgsId);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /global/conf/add
     * @auth globalConfAdd
     * @req Srv.Apps.Proto.DTO.Develop.GlobalConfAddDTO
     * 全局配置新增
     */
    public function globalConfAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof GlobalConfAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $stgsId     = GlobalConfCtl::tag()->globalConfAdd($setData);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        if($stgsId < 1) {
            $errMsg = '全局配置新增失败';
            $LogCtl->logAdd(LogCtl::GLOBAL_CONF_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = '全局配置新增成功';
        $LogCtl->logAdd(LogCtl::GLOBAL_CONF_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /global/conf/edit
     * @auth globalConfEdit
     * @req Srv.Apps.Proto.DTO.Develop.GlobalConfEditDTO
     * 全局配置编辑
     */
    public function globalConfEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof GlobalConfEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $stgsId         = intval($DTOClass->getStgsId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $GlobalConfCtl  = GlobalConfCtl::tag();
        $LogCtl         = LogCtl::tag();
        $globalConfSrc  = $GlobalConfCtl->getGlobalConfSrc($stgsId);
        if(!isset($globalConfSrc['stgs_id'])) {
            $logData    = $setData;
            $errMsg     = '全局配置新增失败';
            $LogCtl->logAdd(LogCtl::GLOBAL_CONF_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
        }
        $result         = $GlobalConfCtl->globalConfEdit($stgsId, $setData);
        $logData        = ['new' => $setData, 'old' => $globalConfSrc];
        if(!$result){
            $errMsg     = '全局配置新增失败';
            $LogCtl->logAdd(LogCtl::GLOBAL_CONF_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '全局配置编辑成功';
        $LogCtl->logAdd(LogCtl::GLOBAL_CONF_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /global/conf/app
     * @auth globalConfApp
     * @rsp Srv.Apps.Proto.VO.Develop.AppGlobalConfVO
     * App全局配置
     */
    public function getAppGlobalConf(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($RouteConfData, $DTOClass);
        if(!($VOClass instanceof AppGlobalConfVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $VOClass        = GlobalConfCtl::tag()->getAppGlobalConf();
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /global/conf/app/edit
     * @auth globalConfAppEdit
     * @req Srv.Apps.Proto.DTO.Develop.AppGlobalConfDTO
     * App全局配置编辑
     */
    public function setAppGlobalConf(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof AppGlobalConfDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $resData        = $RouteConfData->getParamsResData($DTOClass);
        $setData        = [];
        $GlobalConfCtl  = GlobalConfCtl::tag();
        $LogCtl         = LogCtl::tag();
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appAdOpen)) $setData[GlobalConfCtl::KEY_APP_AD_OPEN] = $resData[AppGlobalConfDTO::_appAdOpen][AppAdOpenDTO::_status];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appAdUnlockSet)) $setData[GlobalConfCtl::KEY_SKIT_AD_UNLOCK_SET] = $resData[AppGlobalConfDTO::_appAdUnlockSet];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appVipUnlockSet)) $setData[GlobalConfCtl::KEY_SKIT_VIP_UNLOCK_SET] = $resData[AppGlobalConfDTO::_appVipUnlockSet];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appCoinUnlockSet)) $setData[GlobalConfCtl::KEY_SKIT_BUY_UNLOCK_SET] = $resData[AppGlobalConfDTO::_appCoinUnlockSet];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appPayRechargeText)) $setData[GlobalConfCtl::KEY_APP_PAY_TEXT_RECHARGE] = $resData[AppGlobalConfDTO::_appPayRechargeText][AppPayRechargeTextDTO::_text];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appPayVip)) $setData[GlobalConfCtl::KEY_APP_PAY_VIP] = $resData[AppGlobalConfDTO::_appPayVip][AppPayVipDTO::_status];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appPayRecharge)) $setData[GlobalConfCtl::KEY_APP_PAY_RECHARGE] = $resData[AppGlobalConfDTO::_appPayRecharge][AppPayRechargeDTO::_status];
	    if($RouteConfData->isSetParam(AppGlobalConfDTO::_appTaskConf)) $setData[GlobalConfCtl::KEY_APP_TASK_STATUS] = $resData[AppGlobalConfDTO::_appTaskConf][AppTaskConfDTO::_status];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appAdMobConf)) $setData[GlobalConfCtl::KEY_APP_AD_MOB_CONF] = $resData[AppGlobalConfDTO::_appAdMobConf];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appRechargeRetain)) $setData[GlobalConfCtl::KEY_APP_RECHARGE_RETAIN] = $resData[AppGlobalConfDTO::_appRechargeRetain];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appRmdAdPos)) $setData[GlobalConfCtl::KEY_APP_RMD_AD_POS] = $resData[AppGlobalConfDTO::_appRmdAdPos];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appReviewFeedback)) $setData[GlobalConfCtl::KEY_APP_REVIEW_FEEDBACK] = $resData[AppGlobalConfDTO::_appReviewFeedback];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appDesktopReward)) $setData[GlobalConfCtl::KEY_APP_DESKTOP_REWARD] = $resData[AppGlobalConfDTO::_appDesktopReward];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appAdSplitTime)) $setData[GlobalConfCtl::KEY_APP_AD_SPLIT_TIME] = $resData[AppGlobalConfDTO::_appAdSplitTime][AppAdSplitTimeDTO::_adSplitTime];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appAdDayLimit)) $setData[GlobalConfCtl::KEY_APP_AD_DAY_LIMIT] = $resData[AppGlobalConfDTO::_appAdDayLimit][AppAdDayLimitDTO::_adDayLimit];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appAdMaxConf)) $setData[GlobalConfCtl::KEY_APP_AD_MAX_CONF] = $resData[AppGlobalConfDTO::_appAdMaxConf];
        if($RouteConfData->isSetParam(AppGlobalConfDTO::_appBannerShowStyle)){
            $style          = $resData[AppGlobalConfDTO::_appBannerShowStyle][AppBannerShowStyleDTO::_style];
            if(!$GlobalConfCtl->checkAppBannerShowStyle($style)){
                $errMsg     = 'Banner样式'.$style.'配置图未上传';
                $logData    = $resData;
                $LogCtl->logAdd(LogCtl::GLOBAL_APP_CONF_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
                return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
            }
            $setData[GlobalConfCtl::KEY_APP_BANNER_SHOW_STYLE]  = $style;
        }
        if(count($setData) < 1){
            $logData    = $resData;
            $errMsg     = 'App全局配置编辑失败';
            $LogCtl->logAdd(LogCtl::GLOBAL_APP_CONF_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $GlobalConfCtl->setAppGlobalConf($setData);
        $oldData        = $GlobalConfCtl->getAppGlobalConf();
        $logData        = ['old' => $oldData, 'new' => $resData];
        if(!$result){
            $errMsg     = 'App全局配置编辑失败';
            $LogCtl->logAdd(LogCtl::GLOBAL_APP_CONF_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = 'App全局配置编辑成功';
        $LogCtl->logAdd(LogCtl::GLOBAL_APP_CONF_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}